package com.reactim.user.controller;

import com.reactim.user.entity.FriendGroup;
import com.reactim.user.entity.FriendRequest;
import com.reactim.user.entity.UserPrivacySetting;
import com.reactim.user.service.FriendGroupService;
import com.reactim.user.service.FriendRequestService;
import com.reactim.user.service.UserPrivacyService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 好友功能测试控制器
 * 仅用于开发和测试环境
 */
@Slf4j
@RestController
@RequestMapping("/api/test/friends")
@RequiredArgsConstructor
public class FriendTestController {
    
    private final FriendRequestService friendRequestService;
    private final FriendGroupService friendGroupService;
    private final UserPrivacyService userPrivacyService;
    
    /**
     * 测试发送好友请求
     */
    @PostMapping("/test-send-request")
    public ResponseEntity<Map<String, Object>> testSendFriendRequest(
            @RequestParam Long fromUserId,
            @RequestParam Long toUserId,
            @RequestParam(required = false) String message) {
        
        Map<String, Object> result = new HashMap<>();
        
        try {
            FriendRequest request = friendRequestService.sendFriendRequest(fromUserId, toUserId, message);
            result.put("success", true);
            result.put("requestId", request.getId());
            result.put("message", "好友请求发送成功");
            
        } catch (Exception e) {
            result.put("success", false);
            result.put("error", e.getMessage());
        }
        
        return ResponseEntity.ok(result);
    }
    
    /**
     * 测试创建好友分组
     */
    @PostMapping("/test-create-group")
    public ResponseEntity<Map<String, Object>> testCreateGroup(
            @RequestParam Long userId,
            @RequestParam String groupName,
            @RequestParam(required = false) String description) {
        
        Map<String, Object> result = new HashMap<>();
        
        try {
            FriendGroup group = friendGroupService.createGroup(userId, groupName, description);
            result.put("success", true);
            result.put("groupId", group.getId());
            result.put("message", "好友分组创建成功");
            
        } catch (Exception e) {
            result.put("success", false);
            result.put("error", e.getMessage());
        }
        
        return ResponseEntity.ok(result);
    }
    
    /**
     * 测试获取用户分组
     */
    @GetMapping("/test-get-groups")
    public ResponseEntity<Map<String, Object>> testGetUserGroups(@RequestParam Long userId) {
        Map<String, Object> result = new HashMap<>();
        
        try {
            List<FriendGroup> groups = friendGroupService.getUserGroups(userId);
            result.put("success", true);
            result.put("groups", groups);
            result.put("count", groups.size());
            
        } catch (Exception e) {
            result.put("success", false);
            result.put("error", e.getMessage());
        }
        
        return ResponseEntity.ok(result);
    }
    
    /**
     * 测试更新隐私设置
     */
    @PostMapping("/test-update-privacy")
    public ResponseEntity<Map<String, Object>> testUpdatePrivacy(
            @RequestParam Long userId,
            @RequestParam(required = false) String friendRequestPermission,
            @RequestParam(required = false) Boolean requireVerification) {
        
        Map<String, Object> result = new HashMap<>();
        
        try {
            UserPrivacySetting setting = new UserPrivacySetting();
            
            if (friendRequestPermission != null) {
                setting.setFriendRequestPermission(
                        UserPrivacySetting.FriendRequestPermission.valueOf(friendRequestPermission));
            }
            if (requireVerification != null) {
                setting.setRequireVerification(requireVerification);
            }
            
            UserPrivacySetting updatedSetting = userPrivacyService.updatePrivacySetting(userId, setting);
            result.put("success", true);
            result.put("setting", updatedSetting);
            result.put("message", "隐私设置更新成功");
            
        } catch (Exception e) {
            result.put("success", false);
            result.put("error", e.getMessage());
        }
        
        return ResponseEntity.ok(result);
    }
    
    /**
     * 测试检查是否可以发送好友请求
     */
    @GetMapping("/test-can-send-request")
    public ResponseEntity<Map<String, Object>> testCanSendRequest(
            @RequestParam Long fromUserId,
            @RequestParam Long toUserId) {
        
        Map<String, Object> result = new HashMap<>();
        
        try {
            boolean canSend = friendRequestService.canSendFriendRequest(fromUserId, toUserId);
            result.put("success", true);
            result.put("canSend", canSend);
            
        } catch (Exception e) {
            result.put("success", false);
            result.put("error", e.getMessage());
        }
        
        return ResponseEntity.ok(result);
    }
    
    /**
     * 测试清理过期请求
     */
    @PostMapping("/test-cleanup-expired")
    public ResponseEntity<Map<String, Object>> testCleanupExpired() {
        Map<String, Object> result = new HashMap<>();
        
        try {
            int cleanedCount = friendRequestService.cleanupExpiredRequests();
            result.put("success", true);
            result.put("cleanedCount", cleanedCount);
            result.put("message", "过期请求清理完成");
            
        } catch (Exception e) {
            result.put("success", false);
            result.put("error", e.getMessage());
        }
        
        return ResponseEntity.ok(result);
    }
}